﻿using Application.Features.Organizations.Commands.Add;
using Application.Features.Organizations.Commands.Delete;
using Application.Features.Organizations.Commands.Update;
using Application.Features.Organizations.DTOs;
using Application.Features.Organizations.Queries.GetAll;
using Common.Models;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;

namespace WebAPI.Controllers;

/// <summary>
/// 组织机构
/// </summary>
[Description("组织机构")]
public class OrganizationController : ApiControllerBase
{
    /// <summary>
    /// 全部查询
    /// </summary>
    /// <returns></returns>
    [HttpPost("query/all")]
    [Description("全部查询")]
    public async Task<Result<IEnumerable<OrganizationOutput>>> GetAllOrganizationsQuery()
    {
        var data = await Mediator.Send(new GetAllOrganizationsQuery());
        return await Result<IEnumerable<OrganizationOutput>>.SucceedAsync(data);
    }

    /// <summary>
    /// 创建
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Description("创建")]
    public async Task<Result> AddAsync(AddOrganizationCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="id">修改组织机构唯一标识</param>
    /// <param name="command">修改数据</param>
    /// <returns></returns>
    [HttpPut("{id}")]
    [Description("修改")]
    public async Task<Result> UpdateAsync(long id, UpdateOrganizationCommand command)
    {
        command.OrganizationId = id;
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("delete/{id}")]
    [Description("删除")]
    public async Task<Result> DeleteAsync(long id)
    {
        await Mediator.Send(new DeleteOrganizationCommand { OrganizationId = id });
        return await Result.SucceedAsync();
    }
}
